<template>
  <ul class="film-info" v-if="film">
    <li class="film-title">
      <div class="film-name">
        {{ film.name }}<span>{{ film.filmType.name }}</span>
      </div>
      <div class="film-grade" v-if="film.grade">{{film.grade}}<span>分</span></div>
    </li>
    <li class="film-category">{{ computedCategory }}</li>
    <li class="film-premiere-time">{{ premiereTime }}上映</li>
    <li class="film-nation-runtime">{{ nationRuntime }}</li>
    <li class="film-synopsis" :class="filmSynopsisClass" ref="dynamicSynopsis">
      {{ film.synopsis }}
    </li>
    <li class="synopsis-toggle-bar" @click="toggleSynopsis">
      <i class="iconfont" :class="toggleBarClass"></i>
    </li>
  </ul>
</template>

<script>
import { mapState } from 'vuex'
import { formatDate } from '@/utils'

export default {
  name: 'FilmInfo',
  data () {
    return {
      isShowAllSynopsis: true,
      synopsisHeight: 0
    }
  },
  computed: {
    ...mapState('FilmModule', ['film']),
    computedCategory () {
      if (!this.film.category) return '未知类型'
      return this.film.category.split('|').join(' | ')
    },
    premiereTime () {
      return formatDate(this.film.premiereAt * 1000, 'YYYY-MM-DD')
    },
    nationRuntime () {
      const nation = this.film.nation ? this.film.nation : '未知地区'
      const runtime = this.film.runtime ? this.film.runtime : '--'
      return `${nation} | ${runtime}分钟`
    },
    filmSynopsisClass () {
      return this.isShowAllSynopsis ? '' : 'film-synopsis-hide'
    },
    toggleBarClass () {
      return this.isShowAllSynopsis ? 'icon-shang' : 'icon-xia'
    }
  },
  methods: {
    // 配合 film-synopsis-hide 类，实现详情信息的展开/收缩效果
    toggleSynopsis () {
      this.isShowAllSynopsis = !this.isShowAllSynopsis
    }
  },
  watch: {
    film () {
      // 动态绑定影片详情高度
      // 原理：监听 film 的变化，等页面渲染完成后，先展开详情，获取到元素高度，再将详情收缩
      // 这样就可以配合 film-synopsis-hide 类，实现详情信息的展开/收缩效果
      this.$nextTick(() => {
        this.isShowAllSynopsis = true
        this.$refs.dynamicSynopsis.style.height = this.$refs.dynamicSynopsis.offsetHeight + 'px'
        this.isShowAllSynopsis = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.film-info {
  padding: 15px;
  font-size: 13px;
  color: #797d82;
  li {
    margin-top: 4px;
  }
  .film-title {
    font-size: 18px;
    color: #191a1b;
    margin-top: 0px;
    display: flex;
    justify-content: space-between;
    span {
      font-size: 12px;
    }
    .film-name {
      display: flex;
      align-items: center;
    }
    .film-name span {
      font-size: 9px;
      color: #fff;
      background-color: #d2d6dc;
      height: 14px;
      line-height: 14px;
      padding: 0 2px;
      border-radius: 2px;
      margin-left: 6px;
      margin-top: 2px;
    }
    .film-grade {
      font-style: italic;
      color: #ffb232;
    }
  }
  .film-synopsis {
    margin-top: 12px;
    line-height: 20px;
    transition: 0.5s ease;
    overflow: hidden;
  }
  .film-synopsis-hide {
    height: 38px !important;
  }
  .synopsis-toggle-bar {
    margin-top: 0px;
    text-align: center;
    cursor: pointer;
  }
  .synopsis-toggle-bar .iconfont {
    font-size: 10px;
  }
}
</style>
